<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Http\Requests\Admin\ActivityRequest;
use App\Models\Activity;
use App\Services\OSS;
use App\Http\Requests\Admin\ActivityeditRequest;

class ActivityController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        //
        $title = '活动位列表';
           // 获取传值得参数
        $count = $request -> input('count',4);
        $search = $request -> input('search','');
        $request = $request -> all();
        // 获取一页展示几条信息
        $data = Activity::where('keywords','like','%'.$search.'%')->paginate($count);
        
        return view('admin.activity.index',compact('title','data','search','count','request'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
        $title = '新增活动位商品';
        return view('admin.activity.create',compact('title'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(ActivityRequest $request)
    {
        //
        // 处理文件名称
          // 实现图片的上传

        if($request -> hasFile('photo')){
        $temp_name = md5(time()+rand(1000,9999));
        // 获取后缀
        $file = $request -> file('photo');
        $hz = $file ->getClientOriginalExtension();
         // 文件路径
        $temp_path = 'uploads/'.date('Ymd',time()); 
        // 拼接文件名
        $name = ltrim($temp_path.'/'.$temp_name.'.'.$hz,'.');
        $temp_name2 = 'uploads/'.$temp_name.'.'.$hz;
        $temp = $temp_name.'.'.$hz;
        //文件上传
        //$request -> file('ppath') -> move($temp_path,$temp_name2);

        // 获取图片在临时文件中的地址
       
        $pic = $file->getRealPath();

        //阿里 OSS 图片上传
        $result = OSS::upload($temp_name2, $pic);

        $data = $request -> except('_token');
        $data['photo'] = $temp;
        $data = Activity::create($data);

        // 判断 是否成功
        if($data){
            return redirect('/admin/activity')->with('success','添加成功');
        }else{
            return back()->withInput('error','添加失败');
        }
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
        $title = '修改活动位信息';
        $data = Activity::find($id);
        return view('admin.activity.edit',compact('title','data'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(ActivityeditRequest $request, $id)
    {
        //
        //若修改了轮播图
        // 处理文件名称
        // dd($data);
        if($request -> hasFile('photo')){
            $temp_name = md5(time()+rand(1000,9999));
            // 获取后缀
            $file = $request -> file('photo');
            $hz = $file ->getClientOriginalExtension();
             // 文件路径
            $temp_path = 'uploads/'.date('Ymd',time()); 
            $temp = $temp_name.'.'.$hz;
            // 拼接文件名
            $name = ltrim($temp_path.'/'.$temp_name.'.'.$hz,'.');
            $temp_name2 = 'uploads/'.$temp_name.'.'.$hz;
            //文件上传
            // $request -> file('ppath') -> move($temp_path,$temp_name2);

            // 获取图片在临时文件中的地址
        
            $pic = $file->getRealPath();

            //阿里 OSS 图片上传
            $result = OSS::upload($temp_name2, $pic);
            
            // 修改
            $activity = Activity::find($id);
            // 删除原来的图片
            $dd = $activity->photo;
            $ossKey = 'uploads/'.$dd;
            $aa = OSS::deleteObject($ossKey);

            $activity->keywords = $request['keywords'];
            $activity->photo = $temp;
            $activity->description = $request['description'];
            $activity->link = $request['link'];
            $activity->status = $request['status'];

            

            // 判断是否成功
            if($activity->save()){
                return redirect('/admin/activity')->with('success','修改成功');
            }else{
                return back()->withInput('error','修改失败');
            }
        }
        // 若没有修改轮播图
        // 修改
            $activity = Activity::find($id);
            $activity->keywords = $request['keywords'];
            $activity->description = $request['description'];
            $activity->link = $request['link'];
            $activity->status = $request['status'];

            // 判断是否成功
            if($activity->save()){
                return redirect('/admin/activity')->with('success','修改成功');
            }else{
                return back()->withInput('error','修改失败');
            }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
         //获取要删除的数据
        $activity = Activity::find($id);

        $data = [];
        $re = $activity -> delete();
        // 判断是否成功
        if($re){
            $data['status'] = 0;
            $data['msg'] = '删除成功';
        }else{
            $data['status'] = 1;
            $data['msg'] = '删除成功';
        }
        return $data;
    }
}
